﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CardInChina.Web.Mvc;
using CardInChina.Bussiness.Frontend.Interface;
using CardInChina.Bussiness.Frontend.Models;

namespace CardInChina.Web.Mvc.Controllers
{
    //deal with favorite operation
    public class FavoriteController : CICBasicController
    {
        private UserService us = new UserService();
        //
        // Add : add a favorite
        [HttpAjaxPost]
        [SessionAuthorizeByAjax]
        public ViewResult Add(int cardID)
        {
            try
            {
                FavoriteOperationResult result = us.AddFavoriteForCustomer(CustomerInfo.CustomerID, cardID);
                if (result  == FavoriteOperationResult.Success)
                    return View("~/Views/Favorite/AddSuccess.cshtml");
                else if (result == FavoriteOperationResult.Duplicate)
                {
                    ViewBag.FailedReason = "您已收藏过该商品";
                    return View("~/Views/Favorite/AddFailed.cshtml");
                }
                else
                {
                    ViewBag.FailedReason = "收藏该商品失败";
                    return View("~/Views/Favorite/AddFailed.cshtml");
                }

            }
            catch(Exception ex)
            {
                ViewBag.FailedReason = "收藏该商品失败";
                ViewBag.Exception = ex.ToString();
                return View("~/Views/Favorite/AddFailed.cshtml");
            }

        }

        [HttpAjaxPost]
        [SessionAuthorizeByAjax]
        public ViewResult Delete(int favoriteID,int cardID)
        {
            try
            {
                FavoriteOperationResult result = us.DeleteFavorite(favoriteID,CustomerInfo.CustomerID, cardID);
                if (result == FavoriteOperationResult.Success)
                    return View("~/Views/Favorite/DeleteSuccess.cshtml");
                else if (result == FavoriteOperationResult.Illegal)
                {
                    ViewBag.FailedReason = "非法操作";
                    return View("~/Views/Favorite/DeleteFailed.cshtml");
                }
                else
                {
                    ViewBag.FailedReason = "取消失败";
                    return View("~/Views/Favorite/DeleteFailed.cshtml");
                }

            }
            catch (Exception ex)
            {
                ViewBag.FailedReason = "取消失败";
                ViewBag.Exception = ex.ToString();
                return View("~/Views/Favorite/DeleteFailed.cshtml");
            }

        }

    }
}
